package com.jackwoo.jerrymouse.http;

import com.jackwoo.jerrymouse.threads.HttpServerThread;
import com.jackwoo.jerrymouse.util.MagicNumberUtil;
import com.sun.source.tree.UsesTree;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author: StupidZhe
 * Date: 2019/11/20
 * Desc: 抽象化HttpResponse
 * HTTP/1.1 304 Not Modified
 * Last-Modified: Tue, 19 Nov 2019 16:24:46 GMT
 * Server: JerryMouse
 * X-Cname-TryFiles: True
 * X-Deity: web03
 * Date: Wed, 20 Nov 2019 06:05:23 GMT
 **/
public class Response {

    private static SimpleDateFormat sdf = new SimpleDateFormat("EEE',' dd MM yyyy HH:mm:ss 'GMT'", Locale.US);

    private String version = "HTTP/1.1";
    private String code = "404";
    private String expression = "Not Found";
    private Map<String, String> params = new ConcurrentHashMap<>(10);
    private byte[] content;
    private HttpServer.ParseResult parseResult;

    public Response(HttpServer.ParseResult content, int code) {

        this(content.content);
        parseResult = content;
        this.code = String.valueOf(code);
        this.addDefaultParam();

        switch (code) {
            case 200:
                expression = "okk";
                break;
            case 300:
                expression = "redirect";
                break;
            default:
                ;
        }

        // 解析路由
        if (content.contentType != null)
            switch (content.contentType) {
                case png: {
                    params.put("Content-Type", "image/png");
                    params.put("accept-ranges", "bytes");
                    break;
                }
                case jpg: {
                    params.put("Content-Type", "image/jpg");
                    params.put("accept-ranges", "bytes");

                    break;
                }
                case gif: {
                    params.put("Content-Type", "image/gif");
                    params.put("accept-ranges", "bytes");
                    break;
                }
                default: {
                }
            }

    }

    private void parseRouter(String router) {
    }

    public Response(byte[] content) {
        this.content = content;
    }


    public static Response generate404(String content404) {

        Response response = new Response(new HttpServer.ParseResult(content404.getBytes()), 404);
        return response;
    }


    private void addDefaultParam() {
        params.put("Last-Modified", sdf.format(new Date()));
        params.put("Date", sdf.format(new Date()));
        params.put("Server", "JerryMouse");
        params.put("Content-Type", "text/html; charset=UTF-8");

    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        if (parseResult.contentType == null) {
            stringBuilder.append(version).append(" ").append(code).append(" ").append(expression).append("\n");
            for (Map.Entry<String, String> entry : params.entrySet()) {
                stringBuilder.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
            }

            stringBuilder.append("\n");
        }
        stringBuilder.append(content);

        return stringBuilder.toString();
    }

    public byte[] getBytes() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(version).append(" ").append(code).append(" ").append(expression).append("\n");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            stringBuilder.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
        }
        stringBuilder.append("\n");
        byte[] c = stringBuilder.toString().getBytes();
        byte[] newContent = Arrays.copyOf(c, c.length + content.length);


        System.arraycopy(content, 0, newContent, c.length, content.length);
        return newContent;
    }

    public static void main(String[] args) {
        //Wed, 20 Nov 2019 07:31:53 GMT

        System.out.println(sdf.format(new Date()));
    }
}
